package com.chunayev.numerology.gui.action;

import static com.chunayev.numerology.util.Constants.RESOURCE_BUNDLE;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;

import com.chunayev.numerology.gui.frame.NumerologyFrame;
import com.chunayev.numerology.util.Constants;


public class CalculateAction extends AbstractAction {
    
    private static final long serialVersionUID = -6816892266670329728L;
    private final NumerologyFrame numerologyFrame;
    
    /**
     * @param numerologyFrame
     *            TODO
     */
    public CalculateAction(final NumerologyFrame numerologyFrame) {
        this.numerologyFrame = numerologyFrame;
        putValue(Action.NAME, RESOURCE_BUNDLE
                .getString("action.calculate.name"));
        putValue(Action.MNEMONIC_KEY, KeyEvent.VK_C);
    }
    
    /*
     * (non-Javadoc)
     * @see
     * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
     * )
     */
    @Override
    public void actionPerformed(final ActionEvent e) {
        // statisticsArea.setText("");
        final String text = this.numerologyFrame.getEnteredData().trim();
        final String[] rows = text.split("\n+");
        final List<String> data = new ArrayList<String>();
        for (final String row : rows) {
            final String trimmedRow = row.trim();
            final String cleanedString = trimmedRow.replaceAll(
                    Constants.REGEXP, "");
            if (!cleanedString.trim().isEmpty()) {
                data.add(trimmedRow);
            }
        }
        
        this.numerologyFrame.setData(data);
    }
}